<?php
 /*
  * @author Luong Trong Hieu
  *
 */
?>
<div id="heading">
<?php
echo $this->title;
?>
    <br />
</div>
<script>
function validate(){
    event.returnValue=true;
    
    function checkEmail(email) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)) {
        alert('Please provide a valid email address');
        email.focus();
        return false;
        }
    }

    function checkBYear(bYear){
        if(bYear.value.length != 4){
            return false;
        }
        
    }
    
    if(accForm.username.value == '')
    {
        alert("Username is required!");
        accForm.username.focus();
        event.returnValue=false;
    }
    if(accForm.password.value == '')
    {
        alert("Password is required!");
        accForm.password.focus();
        event.returnValue=false;
    }
    
    if(!(accForm.sex[0].checked) && !(accForm.sex[1].checked))
    {
        alert("Sex is required!");
        event.returnValue=false;
    }
    
    if(!checkEmail(accForm.email))
    {
        event.returnValue=false;
    }

    if(checkBYear(accForm.byear)==false)
    {
        alert("Please provide a valid Year of birth!");
        accForm.byear.focus();
        event.returnValue=false;
    }
}
</script>

<?php
if(!isset($_POST['submit']))
{
?>

<div id="form">
<form method="POST" action="" name="accForm" onsubmit="validate();">
    Username: (*) <br /><input type="text" name="username"><br />
    Password: (*)<br /><input type="password" name="password"> <br />
    FullName: <br /><input type="text" name="fullname"> <br />
    Year of Birth: <br /> <input type="text" name="byear"> <br />
    Sex: (*) <br /><input type="radio" name="sex" value="1" checked="checked"> Male
    <input type="radio" name="sex" value="0"> Female <br />
    Email: (*) <br /><input type="text" name="email"><br/>
    Address: <br /><input type="text" name="address"><br />
    <input type="hidden" name="roleid" value="1">
    <input type="hidden" name="typeid" value="1">
    <input type="hidden" name="status" value="available">
    <br />
    <input type="submit" name="submit" value="Add">
    <input type="reset" name="reset" value="Clear!">
 </form>
</div>
<?php
}
else
{
    echo "<center>";
    echo "$this->message<br />";
    if($this->success)
    {
        echo "<meta http-equiv='refresh' content='2; url=".
            $this->url(array('controller' => 'index', 'action'=> 'login')).
            "' />";
        echo "<a href='>".$this->url(array('controller' => 'index',
                'action'=> 'login'))."'>Click here if browser does not
                automatically redirect you.</a><br />";
    }
    else
    {
        echo "<a href='javascript:history.go(-1)'>Go back</a><br />";
    }
    echo "</center>";
}
?>